package com.company.api.service;

import com.company.api.entity.Goods;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * 服务降级（用于客户端），用于处理GoodsClientService中的服务出现异常时的
 * 注意当前类需要添加@Component注解
 */

@Component
public class GoodsClientServiceFallbackFactory implements FallbackFactory<GoodsClientService> {

    private static final Logger logger = LoggerFactory.getLogger(GoodsClientServiceFallbackFactory.class);

    @Override
    public GoodsClientService create(Throwable throwable) {
        return new GoodsClientService() {
            @Override
            public List<Goods> getGoods() {
                logger.warn("调用服务出现异常，现进行服务降级处理");
                return Collections.singletonList(new Goods("服务降级ID", "服务降级名称"));
            }
        };
    }
}
